package com.isoftstone.util;

import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.*;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * Title: CommUtil.java
 * </p>
 * <p>
 * <p>
 * Description: 系统工具类
 * </p>
 * <p>
 * <p>
 * Copyright: Copyright (isoftstone) 2016
 * </p>
 */
public class BocHttpUtil {


    public static Logger logger = LoggerFactory.getLogger(BocHttpUtil.class);

    private static String paymentContentPacketPrefix = "00E1091500010400016               382013110102023899999990                                                                         00471LI0016";

    /**
     * 获取绑定银行卡下送到卡列表
     *
     * @param card 01 借记卡  02信用卡
     * @return
     */
    public static List<Map<String, String>> getBocCard(
            HttpServletRequest request, String card) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map;
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("getCardList");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================

            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("custNo",
                    (String) request.getSession().getAttribute("userid"));
            obj.element("elecCardFlag", "2");  //获取1类2类3类卡
            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

            jsonobject = JSONObject.fromObject(buffer.toString());
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> maps = mapper.readValue(jsonobject.toString(),
                    Map.class);
            List<Map<String, Object>> custCardList = (List) maps
                    .get("custCardList");
            if (custCardList != null && custCardList.size() != 0) {
                for (int i = 0; i < custCardList.size(); i++) {
                    Map<String, Object> cardMap = custCardList.get(i);
                    String bindType = (String) cardMap.get("bindType");
                    String bankId = (String) cardMap.get("bankId");  // 中行卡bankId是104
                    //借记卡01 96
                    // bindType != null && bindType.equals(card)
                    if (StringUtils.equals(bankId, "104")) { //104 中行卡
                        map = new HashMap<String, String>();
                        map.put("bindType", bindType);
                        /**
                         * 返回带*号的数据给前端
                         */
                        map.put("cardNo", (String) cardMap.get("screenCardNo"));
                        map.put("acctNo", (String) cardMap.get("cardNo"));
                        map.put("actName", (String) cardMap.get("actName"));
                        map.put("cardSeq", (String) cardMap.get("cardSeq"));
                        map.put("elecCardFlag", (String) cardMap.get("elecCardFlag"));
                        map.put("elecCardType", (String) cardMap.get("elecCardType"));
                        String bindtime = null;
                        if (cardMap.get("bindTime") != null) {
                            bindtime = String.valueOf(((LinkedHashMap) cardMap.get("bindTime")).get("time"));
                        }
                        map.put("bindTime", bindtime);
                        list.add(map);
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
            throw new RuntimeException("网络异常!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据信用卡号 获取未出和已出账单
     * 1 未出账单  2 已出账单
     *
     * @param request
     * @return
     */
    public static List<Map<String, String>> getCardBill(
            HttpServletRequest request, String card) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map;
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("getCardList");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            //请求数据
            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

            jsonobject = JSONObject.fromObject(buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> maps = mapper.readValue(jsonobject.toString(),
                    Map.class);
            for (Map.Entry<String, Object> entry : maps.entrySet()) {
                logger.info("############# = " + entry.getKey());
                logger.info("############# = " + entry.getValue());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 发送扣款信息到陕西分行  校园卡充值
     *
     * @param request
     * @return
     */
    public static Map<String, String> sendBocSX(Integer Id, HttpServletRequest request) {

        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        Map<String, String> map = new HashMap<>();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();
            String urls = PropertiesUtils.getProperties("mciscsp");
            URL url = new URL(urls);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            //学校编号 系统头里边
            String schoolNo = request.getParameter("cars");

            //报文体
            //学号
            String cusNo = request.getParameter("schoolCardNum");
            //用户姓名
            String userName = request.getParameter("userName");
            //证件号码
            String Cus_passno = (String) request.getSession().getAttribute("Cus_passno");
            //金额
            double money = Double.valueOf(request.getParameter("rechargeAmount"));
            //money = money*100;
            //int m =(int) money;
            //卡号
            String cardNo = request.getParameter("cardNo");
            //易商账号
            String bocUser = (String) request.getSession().getAttribute("userid");

            String msgs = "00E1091500010400027               38201402211715421392974142713858                                                                 00210LI0027"
                    + "<?xml version=\"1.0\" encoding=\"utf-8\"?><UTILITY_PAYMENT><CONST_HEAD><REQUEST_TYPE>0240</REQUEST_TYPE><TRN_CODE>ESCH02</TRN_CODE><AGENT_CODE>" + schoolNo + "</AGENT_CODE><TELLER_CODE>9880800</TELLER_CODE><CUST_IP>127.0.0.1</CUST_IP></CONST_HEAD><DATA_AREA><Cus_no>" + cusNo + "</Cus_no><Real_pay>" + money + "</Real_pay><Cus_name>" + userName + "</Cus_name><Cus_passno>" + Cus_passno + "</Cus_passno><Card_type>2</Card_type><Card_no>" + cardNo + "</Card_no><Free_use1>" + bocUser + "</Free_use1></DATA_AREA></UTILITY_PAYMENT>";
            outputStream.write(msgs.getBytes("GBK"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "GBK");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            //解析xml
            try {
                Document doc = DocumentHelper.parseText(buffer.toString().substring(136));
                Map maps = XmlUtils.Dom2Map(doc);
                map = (Map<String, String>) maps.get("CONST_HEAD");
                if ("00".equals(map.get("ERR_CODE"))) {
                    map = (Map<String, String>) maps.get("DATA_AREA");
                    //结果正常添加成功返回码000000
                    map.put("ERR_CODE", "000000");
                }
            } catch (Exception e) {
                e.printStackTrace();
                //错误 999999
                map.put("ERR_CODE", "999999");
                map.put("ERR_MSG", "交易失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("ERR_CODE", "999999");
            map.put("ERR_MSG", "网络通讯异常");
        }
        return map;
    }


    /**
     * 发送缴费信息，进行支付扣款
     *
     * @param request
     */
    public static Map<String, String> sendkjPay(String sendXml, HttpServletRequest request) {

        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        Map<String, String> map = new HashMap<>();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();
            String urls = PropertiesUtils.getProperties("mciscsp");
            URL url = new URL(urls);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            String msgs = paymentContentPacketPrefix + sendXml;
            outputStream.write(msgs.getBytes("utf-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            //解析xml
            try {
//                map = DocumentReader.xmlReaderMessage(buffer.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取验证码
     *
     * @param request
     */
    public static void getCode(HttpServletRequest request) {
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();
            String urls = PropertiesUtils.getProperties("getCode");
            URL url = new URL(urls);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("usrid",
                    (String) request.getSession().getAttribute("userid"));
            obj.element("usrtel", (String) request.getSession().getAttribute(Const.MOBLENO));
            //obj.element("randtrantype","PD0004");
            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info("获取验证码接口" + buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 验证码发送到boc验证正确性
     *
     * @param request
     * @return
     */

    public static String verifyCode(HttpServletRequest request) {
        StringBuffer buffer = new StringBuffer();
        Map<String, String> map = new HashMap<>();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String urls = PropertiesUtils.getProperties("verifyCode");
            URL url = new URL(urls);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("usrid",
                    (String) request.getSession().getAttribute("userid"));
            obj.element("usrtel", (String) request.getSession().getAttribute(Const.MOBLENO));
            //obj.element("randtrantype","PD0004");
            obj.element("mobcheck", (String) request.getParameter("code"));

            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info("验证验证码 结果返回" + buffer.toString());
            //{"msgcde":"M321","rtnmsg":"短信验证不通过！[B03_00015]"}
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();


            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(buffer.toString(), Map.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (map.get("usrid") != null && map.get("usrid").equals((String) request.getSession().getAttribute("userid"))) {
            return "000000";
        }
        if (map.get("rtnmsg") != null) {
            return map.get("rtnmsg");
        } else {
            return "验证失败!";
        }
    }

    /**
     * 开放平台卡转账到卡 亲情汇接口
     *
     * @param request
     * @return
     */
    public static String sendCard(Integer id, HttpServletRequest request) {
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String urls = PropertiesUtils.getProperties("toCard");
            URL url = new URL(urls);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();


            // 请求数据
            obj.element("userid_uid", (String) request.getParameter(""));
            obj.element("uid_param", (String) request.getParameter(""));
            obj.element("inCustName", (String) request.getParameter(""));
            obj.element("systemUuid", id.toString());
            obj.element("systemFlag", (String) request.getParameter(""));
            obj.element("channelFlag", (String) request.getParameter(""));
            obj.element("clientId", (String) request.getParameter(""));
            obj.element("trsSubType", (String) request.getParameter(""));
            obj.element("outCardNo", (String) request.getParameter(""));
            obj.element("outCardSeq", (String) request.getParameter(""));
            obj.element("inCardNo", (String) request.getParameter(""));
            obj.element("trsAmt", (String) request.getParameter(""));
            obj.element("ccy", (String) request.getParameter(""));
            obj.element("ccy", (String) request.getParameter(""));


            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前用户的 个人信息 from BOC 核心系统
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> queryUserInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        StringBuffer buffer = new StringBuffer();
        try {
             /*
             String urls = PropertiesUtils.getProperties("queryUserInfo");
			 URL url = new URL(urls);
		     HttpURLConnection	httpUrlConn = HttpConnectionUtil.getHttpsConnection(url);
              */

            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String urls = PropertiesUtils.getProperties("queryUserInfo");
            URL url = new URL(urls);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();


            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================

            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);


            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            outputStream.write("".getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info("查询个人信息" + buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(buffer.toString(), Map.class);
            String mobileno = map.get("mobileno");
            if (mobileno != null && !"".equals(mobileno)) {
                request.getSession().setAttribute("mobileno", mobileno);
            }

        } catch (SocketException st) {
            st.printStackTrace();
            throw new RuntimeException("请求超时");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统异常");
        }
        return map;
    }

    /**
     * 查询校园卡 余额
     *
     * @param request
     * @return
     */
    public static Map<String, String> querySchoolCard(HttpServletRequest request) {
        StringBuffer buffer = new StringBuffer();
        Map<String, String> map = new HashMap<>();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String urls = PropertiesUtils.getProperties("mciscsp");
            URL url = new URL(urls);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            //学校编号
            String schoolNo = request.getParameter("cars");
            //学号
            String cusNo = request.getParameter("schoolCardNum");

            String msgs = "00E1091500010400027               38201402211715421392974142713858                                                                 00210LI0027"
                    + "<?xml version='1.0' encoding='UTF-8'?><UTILITY_PAYMENT><CONST_HEAD><REQUEST_TYPE>0240</REQUEST_TYPE><TRN_CODE>ESCH01</TRN_CODE><AGENT_CODE>" + schoolNo + "</AGENT_CODE></CONST_HEAD><DATA_AREA><Cus_no>" + cusNo + "</Cus_no></DATA_AREA></UTILITY_PAYMENT>";
            outputStream.write(msgs.getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "GBK");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            //解析xml
            try {
                Document doc = DocumentHelper.parseText(buffer.toString().substring(136));
                Map maps = XmlUtils.Dom2Map(doc);
                map = (Map<String, String>) maps.get("DATA_AREA");
                //结果正常添加成功返回码000000
                map.put("ERR_CODE", "000000");
            } catch (Exception e) {
                //错误 999999
                map.put("ERR_CODE", "999999");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url   发送请求的 URL
     * @param param 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     * @author add by xc
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 发送POST请求必须设置如下两行
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            osw.write(param);
            osw.flush();
            osw.close();
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            logger.info("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
            result = "";
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


    /**
     * 查询卡信息
     *
     * @param request
     * @return
     */
    public static String getCardInfo(
            HttpServletRequest request, String card) {

        String result = null;

        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map;
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("getCardinfo");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));


            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();


            obj.element("accno", card);
            obj.element("userid", (String) request.getSession().getAttribute("userid"));
            obj.element("pageno", 1);

            //请求数据
            outputStream.write(obj.toString().getBytes("UTF-8"));


            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

            jsonobject = JSONObject.fromObject(buffer.toString());

            result = buffer.toString();

            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> maps = mapper.readValue(jsonobject.toString(),
                    Map.class);
            for (Map.Entry<String, Object> entry : maps.entrySet()) {
                logger.info("############# = " + entry.getKey());
                logger.info("############# = " + entry.getValue());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 借记卡交易查询
     * 根据卡号及日期查询交易明细
     *
     * @param request
     * @param LIMITAMT 唯一标识
     * @param STDATE   开始日期  YYYYMMDD
     * @param ENDATE   结束日期	 YYYYMMDD
     * @param PAGENO   起始记录 第一页送1
     * @return
     */
    public static List<Map<String, String>> debitBankQuery(
            HttpServletRequest request, String LIMITAMT, String STDATE, String ENDATE, String PAGENO) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map;
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("debitbankquery");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("limitamt", LIMITAMT);
            obj.element("stdate", STDATE);
            obj.element("endate", ENDATE);
            obj.element("pageno", PAGENO);
            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

            jsonobject = JSONObject.fromObject(buffer.toString());
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> maps = mapper.readValue(jsonobject.toString(),
                    Map.class);
            List<Map<String, Object>> custCardList = (List) maps
                    .get("saplist");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 借记卡交易查询
     * 根据卡号及日期查询交易明细
     *
     * @param request
     * @param LIMITAMT 唯一标识
     * @param STDATE   开始日期  YYYYMMDD
     * @param ENDATE   结束日期	 YYYYMMDD
     * @param PAGENO   起始记录 第一页送1
     * @return
     */
    public static Map<String, Object> debitBankQueryByLastMonth(
            HttpServletRequest request, String LIMITAMT, String STDATE, String ENDATE, String PAGENO) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("debitbankquery");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("custNo", (String) request
                    .getSession().getAttribute("userid"));
            obj.element("limitamt", LIMITAMT);
            obj.element("stdate", STDATE);
            obj.element("endate", ENDATE);
            obj.element("pageno", PAGENO);
            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;

            jsonobject = JSONObject.fromObject(buffer.toString());
            //jsonobject = JSONObject.fromObject(teststr);
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(jsonobject.toString(),
                    Map.class);

            //list.add(maps);

            /*if (maps!=null && StringUtils.isNotBlank((String)maps.get("record_count"))){

                Integer totalCount = Integer.valueOf((String) maps.get("record_count"));

                if (totalCount != 0) {
                       // 计算页数
                    List<Map<String, Object>> tranList = (List) maps
                            .get("saplist");

                    if(tranList.size()!=0){

                        Double pageIndex =  Math.ceil(totalCount / tranList.size());
                        int  totalPage  =  (new   Double(pageIndex)).intValue();


                        for (int i = 2; i < totalPage; i++) {
                            Map<String, Object> record = setHeaderAndBody(httpUrlConn, LIMITAMT, STDATE, ENDATE, String.valueOf(i));
                            list.add(record);
                        }


                    }
                }


            }*/

            httpUrlConn.disconnect();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return map;
    }


    /**
     * 查询卡余额
     *
     * @param request
     * @param LIMITAMT 卡唯一标示
     * @return
     */
    public static Map<String, Object> debitMultiBlance(
            HttpServletRequest request, String LIMITAMT) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map;
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        Map<String, Object> maps = null;
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String balanceUrl = PropertiesUtils.getProperties("debitmultiblance");
            URL url = new URL(balanceUrl);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("cardSeq", LIMITAMT);
            obj.element("custNo", (String) request
                    .getSession().getAttribute("userid"));
            obj.element("currency", "CNY");
            obj.element("forCurrTran", "0");

            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

            jsonobject = JSONObject.fromObject(buffer.toString());
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            maps = mapper.readValue(jsonobject.toString(),
                    Map.class);
            // List<Map<String, Object>> bal = (List) maps
            //          .get("saplist");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return maps;
    }


    public static Map<String, Object> setHeaderAndBody(HttpsURLConnection httpUrlConn, String LIMITAMT, String STDATE, String ENDATE, String PAGENO) throws Exception {

        OutputStream outputStream = httpUrlConn.getOutputStream();
        // 注意编码格式，防止中文乱码
        JSONObject obj = new JSONObject();
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        // 请求数据
        obj.element("limitamt", LIMITAMT);
        obj.element("stdate", STDATE);
        obj.element("endate", ENDATE);
        obj.element("pageno", PAGENO);
        outputStream.write(obj.toString().getBytes("UTF-8"));
        outputStream.close();
        // 将返回的输入流转换成字符串
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(
                inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(
                inputStreamReader);
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        logger.info(buffer.toString());
        bufferedReader.close();
        inputStreamReader.close();
        // 释放资源
        inputStream.close();
        inputStream = null;

        jsonobject = JSONObject.fromObject(buffer.toString());
        logger.info("buffer = " + buffer.toString());
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> maps = mapper.readValue(jsonobject.toString(),
                Map.class);

        return maps;
    }

    /**
     * 借记卡交易查询
     * 根据卡号及日期查询交易明细
     *
     * @param request
     * @param LIMITAMT 唯一标识
     * @param STDATE   开始日期  YYYYMMDD
     * @param ENDATE   结束日期	 YYYYMMDD
     * @param PAGENO   起始记录 第一页送1
     * @return
     */
    public static Map<String, Object> debitBankQueryByLastMonth1(
            HttpServletRequest request, String LIMITAMT, String STDATE, String ENDATE, String PAGENO) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("debitbankquery1");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("limitamt", LIMITAMT);
            obj.element("stdate", STDATE);
            obj.element("endate", ENDATE);
            obj.element("pageno", PAGENO);
            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;

            jsonobject = JSONObject.fromObject(buffer.toString());
            //jsonobject = JSONObject.fromObject(teststr);
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(jsonobject.toString(),
                    Map.class);

            httpUrlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return map;
    }


    public static Map<String, Object> debitBankAsrQuery(
            HttpServletRequest request, String LIMITAMT, String STDATE, String ENDATE, String PAGENO, String perReco) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("debitbankquery3");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();


            int begReco = Integer.parseInt(perReco) * (Integer.parseInt(PAGENO) - 1) + 1;

            // 请求数据
            obj.element("custNo", (String) request.getSession().getAttribute("userid"));
            obj.element("cardSeq", LIMITAMT);
            obj.element("selBegDate", STDATE);
            obj.element("selCutOfDate", ENDATE);
            obj.element("perReco", perReco);
            obj.element("begReco", begReco);
            obj.element("curr", "CNY");
            obj.element("tradeCode", "C020");
            obj.element("reqSour", "03");
            obj.element("subAccNo", "");
            obj.element("netOper", "BOCOP");


            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;

            //String teststr = "{\"pageno\":\"1\",\"record_count\":\"10\",\"saplist\":[{\"trandate\":\"20170101\",\"trantime\":\"121516\",\"currency\":\"CNY\",\"amt\":\"100\",\"billflag\":\"1\",\"oppsopeno\":\"12345678\", \"oppsopen\":\"测试\",\"oppsopename\":\"测试\",\"oppsopenacc\":\"测试\",\"remark\":\"测试\",\"postscript\":\"测试\",\"reveflag\":\"1\"}]}";
            jsonobject = JSONObject.fromObject(buffer.toString());
            //jsonobject = JSONObject.fromObject(teststr);
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(jsonobject.toString(),
                    Map.class);

            //list.add(maps);

            /*if (maps!=null && StringUtils.isNotBlank((String)maps.get("record_count"))){

                Integer totalCount = Integer.valueOf((String) maps.get("record_count"));

                if (totalCount != 0) {
                       // 计算页数
                    List<Map<String, Object>> tranList = (List) maps
                            .get("saplist");

                    if(tranList.size()!=0){

                        Double pageIndex =  Math.ceil(totalCount / tranList.size());
                        int  totalPage  =  (new   Double(pageIndex)).intValue();


                        for (int i = 2; i < totalPage; i++) {
                            Map<String, Object> record = setHeaderAndBody(httpUrlConn, LIMITAMT, STDATE, ENDATE, String.valueOf(i));
                            list.add(record);
                        }


                    }
                }


            }*/

            httpUrlConn.disconnect();


        } catch (SocketException se) {

            se.printStackTrace();
            throw new RuntimeException("网络异常!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return map;
    }


    public static Map<String, String> exchangeToken(HttpServletRequest request, String oclientid) {


        Map<String, String> map = new HashMap<>();
        StringBuffer buffer = new StringBuffer();
        try {


            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String urls = PropertiesUtils.getProperties("exchageToken");


            String action = (String) request
                    .getSession().getAttribute("accessToken");

            String userid = (String) request
                    .getSession().getAttribute("userid");

            String newurl = urls.replace("PARACTON", action).replace("PARCLIENT_ID", oclientid).replace("PARUSER_ID", userid);


            URL url = new URL(newurl);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);

            /**
             *
             * params.put("enctyp", "");
             params.put("password", "");
             params.put("grant_type", "implicit");
             params.put("user_id", userid);
             params.put("client_id", "205");
             params.put("acton", action);

             */

            //https://openapi.boc.cn/bocop/oauth/token?acton=03633f55-1515-4b17-a0b1-3842a6bf69aa&client_id=205&password=&grant_type=implicit&user_id=15021553067&enctyp=
            // 设置header参数

            httpUrlConn.setRequestProperty("client_id", "507");
            httpUrlConn.setRequestProperty("user_id", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));


            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

//            obj.element("enctyp",  "");
//            obj.element("password", "");
//            obj.element("client_id", "205");
//            obj.element("user_id", (String) request
//                    .getSession().getAttribute("userid"));
//            obj.element("acton", (String) request
//                    .getSession().getAttribute("accessToken"));
//            obj.element("grant_type", "implicit");


            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info("token信息" + buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();

            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(buffer.toString(), Map.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;

    }

    /**
     * 信用卡余额查询
     *
     * @param request
     * @param LIMITAMT 唯一标识
     * @return
     */
    public static Map<String, Object> creditbalsearch(
            HttpServletRequest request, String LIMITAMT) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map;
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        Map<String, Object> maps = null;
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String balanceUrl = PropertiesUtils.getProperties("creditbalsearch");
            URL url = new URL(balanceUrl);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));
            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();
            // 请求数据
            obj.element("LIMITAMT", LIMITAMT);
            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            httpUrlConn.disconnect();

            jsonobject = JSONObject.fromObject(buffer.toString());
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            maps = mapper.readValue(jsonobject.toString(),
                    Map.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return maps;
    }


    /**
     * 标准接口	查询信用卡额度 	1202
     *
     * @param request
     * @param cardSeq 卡唯一标识
     * @return
     */
    public static Map<String, Object> query_creditcard_balance(
            HttpServletRequest request, String cardSeq) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("query_creditcard_balance");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();
            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================
            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("custNo", (String) request.getSession().getAttribute("userid"));
            obj.element("cardSeq", cardSeq);

            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;

            jsonobject = JSONObject.fromObject(buffer.toString());
            //jsonobject = JSONObject.fromObject(teststr);
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(jsonobject.toString(),
                    Map.class);

            httpUrlConn.disconnect();

        } catch (SocketException se) {
            se.printStackTrace();
            throw new RuntimeException("网络异常");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 查询信用卡已出账单
     *
     * @param request
     * @param LIMITAMT
     * @param billDate
     * @param start
     * @param selNum
     * @return
     */
    public static Map<String, Object> query_creditcard_billed(
            HttpServletRequest request, String LIMITAMT, String billDate, String start, String selNum) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("query_creditcard_billed");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("custNo", (String) request.getSession().getAttribute("userid"));
            obj.element("cardSeq", LIMITAMT);
            obj.element("curr", "CNY");
            obj.element("start", start);
            obj.element("selNum", selNum);
            obj.element("billDate", billDate);
            obj.element("channelFlag", "1");

            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;

            //String teststr = "{\"pageno\":\"1\",\"record_count\":\"10\",\"saplist\":[{\"trandate\":\"20170101\",\"trantime\":\"121516\",\"currency\":\"CNY\",\"amt\":\"100\",\"billflag\":\"1\",\"oppsopeno\":\"12345678\", \"oppsopen\":\"测试\",\"oppsopename\":\"测试\",\"oppsopenacc\":\"测试\",\"remark\":\"测试\",\"postscript\":\"测试\",\"reveflag\":\"1\"}]}";
            jsonobject = JSONObject.fromObject(buffer.toString());
            //jsonobject = JSONObject.fromObject(teststr);
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(jsonobject.toString(),
                    Map.class);

            httpUrlConn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return map;
    }


    public static Map<String, Object> query_creditcard_futurebill(
            HttpServletRequest request, String LIMITAMT, String start, String selNum) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonobject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象，并使用我们指定的新人管理器初始化
            TrustManager[] tm = {new MyX509TrustManager()};
            SSLContext sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
            sslcontext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocktFactory对象
            SSLSocketFactory ssf = sslcontext.getSocketFactory();

            String getCardList = PropertiesUtils.getProperties("query_creditcard_futurebill");
            URL url = new URL(getCardList);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
                    .openConnection();

            //==============================================
            httpUrlConn.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });
            //=================================================


            httpUrlConn.setSSLSocketFactory(ssf);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置header参数
            httpUrlConn.setRequestProperty("clentid",
                    PropertiesUtils.getProperties("clientid"));
            httpUrlConn.setRequestProperty("userid", (String) request
                    .getSession().getAttribute("userid"));
            httpUrlConn.setRequestProperty("acton", (String) request
                    .getSession().getAttribute("accessToken"));
            httpUrlConn.setRequestProperty("chnflg", "1");
            Date date = new Date();
            SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat da = new SimpleDateFormat("HHmmss");
            httpUrlConn.setRequestProperty("trandt", d.format(date));
            httpUrlConn.setRequestProperty("trantm", da.format(date));

            // 设置请求方式（GET/POST）
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            // 当有数据需要提交时
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式，防止中文乱码
            JSONObject obj = new JSONObject();

            // 请求数据
            obj.element("custNo", (String) request.getSession().getAttribute("userid"));
            obj.element("cardSeq", LIMITAMT);
            obj.element("curr", "CNY");
            obj.element("start", start);
            obj.element("selNum", selNum);
            obj.element("channelFlag", "1");

            outputStream.write(obj.toString().getBytes("UTF-8"));
            outputStream.close();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            logger.info(buffer.toString());
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;

            //String teststr = "{\"pageno\":\"1\",\"record_count\":\"10\",\"saplist\":[{\"trandate\":\"20170101\",\"trantime\":\"121516\",\"currency\":\"CNY\",\"amt\":\"100\",\"billflag\":\"1\",\"oppsopeno\":\"12345678\", \"oppsopen\":\"测试\",\"oppsopename\":\"测试\",\"oppsopenacc\":\"测试\",\"remark\":\"测试\",\"postscript\":\"测试\",\"reveflag\":\"1\"}]}";
            jsonobject = JSONObject.fromObject(buffer.toString());
            //jsonobject = JSONObject.fromObject(teststr);
            logger.info("buffer = " + buffer.toString());
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(jsonobject.toString(),
                    Map.class);

            httpUrlConn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return map;
    }
}


class MyX509TrustManager implements X509TrustManager {

    public void checkClientTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }


}



